from PyQt5.QtWidgets import (QWidget, QLabel, QVBoxLayout, QHBoxLayout,
                             QPushButton, QRadioButton, QButtonGroup, QFrame,
                             QScrollArea, QTextEdit, QSizePolicy)
from PyQt5.QtCore import Qt
import sys
from io import StringIO
from Data.Chinese import model_number
from Data.English import model_number_en
from Data.Chinese import model
from Data.English import model_en


class OutputInterceptor(StringIO):
    def __init__(self, label):
        super().__init__()
        self.label = label
        self.original_stdout = sys.stdout

    def write(self, text):
        super().write(text)
        current_text = self.label.toPlainText()
        if current_text:
            # 保留换行符，但避免重复换行
            if text.strip():  # 如果不是空行
                self.label.setText(current_text + '\n' + text.rstrip())
        else:
            self.label.setText(text.rstrip())


class ModelPage(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.init_ui()

    def init_ui(self):
        layout = QVBoxLayout(self)
        layout.setContentsMargins(50, 30, 50, 50)

        # 创建标题
        title = QLabel('分册编号')
        title.setAlignment(Qt.AlignCenter)
        title.setStyleSheet('font-size: 30px; color: #2c3e50; margin: 10px 0 30px 0;')

        # 创建单选按钮组
        radio_container = QWidget()
        radio_layout = QHBoxLayout(radio_container)
        radio_layout.setAlignment(Qt.AlignCenter)

        self.button_group = QButtonGroup()

        # 创建单选按钮
        svn_radio = QRadioButton("SVN")
        non_svn_radio = QRadioButton("非SVN")
        non_svn_radio.setChecked(True)

        # 设置按钮样式
        radio_style = """
            QRadioButton {
                font-size: 16px;
                color: #696969;
                padding: 5px;
                spacing: 10px;
                margin-top: 10px;
            }
            QRadioButton::indicator {
                width: 15px;
                height: 15px;
            }
        """
        svn_radio.setStyleSheet(radio_style)
        non_svn_radio.setStyleSheet(radio_style)

        # 将单选按钮添加到按钮组
        self.button_group.addButton(svn_radio)
        self.button_group.addButton(non_svn_radio)

        # 添加到水平布局
        radio_layout.addStretch()  # 添加弹性空间
        radio_layout.addWidget(svn_radio)
        radio_layout.addSpacing(40)  # 按钮之间的间距
        radio_layout.addWidget(non_svn_radio)
        radio_layout.addStretch()  # 添加弹性空间

        # 创建运行按钮
        run_button = QPushButton('运行')
        run_button.setFixedSize(200, 40)
        run_button.setStyleSheet("""
            QPushButton {
                background-color: #1E90FF;
                color: white;
                border: none;
                border-radius: 6px;
                font-size: 16px;
            }
            QPushButton:hover {
                background-color: #c0392b;
            }
            QPushButton:pressed {
                background-color: #a93226;
            }
        """)
        run_button.clicked.connect(self.run_process)

        # 添加分割线
        line = QFrame()
        line.setFrameShape(QFrame.HLine)
        line.setStyleSheet("background-color: #A9A9A9;")
        line.setFixedHeight(2)

        # 创建信息显示区域
        self.info_label = QTextEdit()
        self.info_label.setReadOnly(True)
        self.info_label.setStyleSheet("""
            QTextEdit {
                color: #228B22;
                font-size: 14px;
                font-family: "Microsoft YaHei";
                margin-top: 10px;
                background-color: #F0FFF0;
                border: none;
            }
        """)

        # 创建滚动区域
        scroll_area = QScrollArea()
        scroll_area.setWidget(self.info_label)
        scroll_area.setWidgetResizable(True)
        scroll_area.setStyleSheet("""
            QScrollArea {
                border: none;
                background-color: #F0FFF0;
            }
            QScrollBar:vertical {
                width: 10px;
                background: #F0FFF0;
            }
            QScrollBar::handle:vertical {
                background: #A9A9A9;
                border-radius: 5px;
            }
            QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {
                height: 0px;
            }
        """)
        scroll_area.setMinimumHeight(300)
        scroll_area.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        # 添加所有组件到主布局
        layout.addWidget(title)
        layout.addWidget(radio_container)
        layout.addSpacing(30)
        layout.addWidget(run_button, alignment=Qt.AlignCenter)
        layout.addSpacing(20)
        layout.addWidget(line)
        layout.addWidget(scroll_area, 1)

    def run_process(self):
        self.info_label.clear()
        lang_combo = self.window().lang_combo
        is_chinese = lang_combo.currentText() == '中文'

        # 获取单选框状态
        selected_button = self.button_group.checkedButton()
        is_svn = selected_button.text() == "SVN"

        output_interceptor = OutputInterceptor(self.info_label)
        sys.stdout = output_interceptor

        try:
            if is_chinese:
                if is_svn:
                    model_number.rename_svn()
                else:
                    model_number.rename()
            else:
                if is_svn:
                    model_number_en.rename_svn()
                else:
                    model_number_en.rename()
        finally:
            sys.stdout = output_interceptor.original_stdout
